package com.mcy.view.toolView;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import com.mcy.study.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.LinearLayout;
public class LayerControl extends LinearLayout {
	
	private List<LayerControlItem> mControlItems;
	private Button btnHome;
	private Animation homeRotateRight;
	private Animation homeRotateLeft;
	
	private AtomicBoolean mAnimationActive = new AtomicBoolean(false);
	private boolean rotated = false;

	public LayerControl(Context context) {
		super(context);
		init(context, null, 0);
	}

	public LayerControl(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs, 0);
	}

	public LayerControl(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs, defStyle);
	}
	
	private void init(Context context, AttributeSet attrs, int defStyle){
		btnHome = new Button(context);
		btnHome.setBackgroundResource(R.drawable.main);
        addView(btnHome);
        
        homeRotateLeft = SatelliteAnimationCreator.createHomeButtonAnimation(context);
		homeRotateRight = SatelliteAnimationCreator.createHomeInverseButtonAnimation(context);
		
		Animation.AnimationListener mAnimationListener = new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				mAnimationActive.set(false);
			}
		};
		
		homeRotateLeft.setAnimationListener(mAnimationListener);
		homeRotateRight.setAnimationListener(mAnimationListener);
		
		btnHome.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				LayerControl.this.onClick();
			}
		});
	}
	
	protected void onClick() {
		 if(mAnimationActive.compareAndSet(false, true)){
			 if(!rotated ){
					btnHome.startAnimation(homeRotateLeft);
					for (LayerControlItem item : mControlItems) {
						item.getView().startAnimation(item.getOutAnimation());
					}
			 }else{
					btnHome.startAnimation(homeRotateLeft);
					for (LayerControlItem item : mControlItems) {
						item.getView().startAnimation(item.getInAnimation());
					}
			 }
			 rotated = !rotated;
		 }
	}
	
	private void openItems(){
		if(mAnimationActive.compareAndSet(false, true)){
			if(!rotated){
				btnHome.startAnimation(homeRotateLeft);
				for (LayerControlItem item : mControlItems) {
					item.getView().startAnimation(item.getOutAnimation());
				}
				rotated = !rotated;
			}
		}
	}
	
	private void closeItem(){
		if(mAnimationActive.compareAndSet(false, true)){
			if(rotated){
				btnHome.startAnimation(homeRotateLeft);
				for (LayerControlItem item : mControlItems) {
					item.getView().startAnimation(item.getOutAnimation());
				}
				rotated = !rotated;
			}
		}
	}

	public void addItems(List<LayerControlItem> items){
		mControlItems.addAll(items);
	}

}
